//
//  LoginViewController.swift
//  test-ios
//
//  Created by wyz5204 on 2023/2/28.
//

import UIKit
import SwiftyJSON
import RxSwift
import RxCocoa
import AudioToolbox
import AVFAudio

import CocoaLumberjack
/**
 按照智慧云的登陆模版为框架，进行开发登陆界面 ， 添加网络请求和 mmkv 以及数据库 和 布局的开发
 */
class LoginViewController: ABaseViewController {
    
    private lazy var containerView : UIView = {
        return UIView()
    }()
    
    private lazy var loginTipView : UILoginTipView = {
        return UILoginTipView()
    }()
    
    private lazy var phoneInputView : UIPhoneInputView = {
        return UIPhoneInputView()
    }()
    
    private lazy var codeInputView : UICodeInputView = {
        return UICodeInputView()
    }()
    
    private lazy var loginBt : UIButton = {
        return UIButton().then { view in
            view.setTitle("验证码登录", for: .normal)
            view.backgroundColor = UIColor.blue
            view.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        }
    }()
    
    private lazy var changeUrl = {
        return UIButton().then { view in
            view.setTitleAndColor("切换环境")
        }
    }()
    
    private let loadEvent = BehaviorSubject(value : false)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        // 容器框
        containerView.addTo(self.view){ view  in
            //view.backgroundColor = .red
            view.snp.makeConstraints { make in
                make.left.equalToSuperview().offset(24)
                make.right.equalToSuperview().offset(-24)
                make.top.equalToSuperview().offset(86)
                make.bottom.equalToSuperview().offset(-40)
            }
        }
        // login 登陆介绍
        loginTipView.addTo(containerView) { view in
            view.snp.makeConstraints { make in
                make.top.equalToSuperview()
                make.left.right.equalToSuperview()
                make.height.equalTo(100)
            }
        }
        
        // 手机号输入框
        phoneInputView.addTo(containerView) { view in
            view.snp.makeConstraints { make in
                make.top.equalTo(loginTipView.snp.bottom).offset(36)
                make.left.right.equalToSuperview()
                make.height.equalTo(40)
            }
        }
        
        // 验证码输入框
        codeInputView.addTo(containerView) { view in
            view.snp.makeConstraints { (make) in
                make.top.equalTo(phoneInputView.snp.bottom).offset(10)
                make.left.right.equalToSuperview()
                make.height.equalTo(65)
            }
        }
        
        // 登陆按钮
        loginBt.addTo(containerView) { view in
            view.snp.makeConstraints { make in
                make.top.equalTo(codeInputView.snp.bottom).offset(75)
                make.left.right.equalToSuperview()
                make.height.equalTo(44)
            }
        }
        
        // 切换环境的按钮
        changeUrl.addTo(containerView) { view in
            view.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.bottom.equalToSuperview()
            }
        }
        
        changeUrl.rx.tap.subscribe { [weak self ] event in
            DDLogDebug(" 点击了 切换环境  \(event)")
            let alert = UIAlertController.init(title: "切换环境", message: " qiehun ", preferredStyle: .actionSheet)
            
            alert.addAction(UIAlertAction.init(title:"aaa", style: .default, handler: { (action) in
                DDLogDebug(" 点击了 aaa  \(action)")
                // 打开
                self?.startViewController(RDMainTabBarViewComtroller())
            }))
            
            alert.addAction(UIAlertAction.init(title: "取消", style: .cancel, handler: { action in DDLogDebug(" 点击了 cancel  \(action)")
            }))
            self?.present(alert, animated: true, completion: nil)
            
        }.disposed(by: rx.disposeBag)
        
        // 手机号码输入框的
        let phoneDriver = phoneInputView.textField.rx.text.orEmpty.asDriver()
        
        phoneDriver.drive (onNext :{ value in
            print("phoneDriver" ,value)
        }).disposed(by: rx.disposeBag)
        
        // 验证码
        let codeDriver =  codeInputView.codeField.rx.text.orEmpty.asDriver()
        codeDriver.drive (onNext :{ value in
            print("codeDriver" ,value)
        }).disposed(by: rx.disposeBag)
        
        // 按钮可点击与否
        let isLoginEnabled = Driver.combineLatest(phoneDriver, codeDriver).map { (phone, code) -> Bool in
            return phone.count >= 8 && code.count >= 8
        }.distinctUntilChanged()
       
        isLoginEnabled.drive(loginBt.rx.isEnabled).disposed(by: rx.disposeBag)
        
        
        //登陆点击事件
        loginBt.rx.tap.asDriver().drive (onNext : {[weak self] in
            self?.login("17572695197" , "123123")
            
            
        }).disposed(by: rx.disposeBag)
        
        // loading加载
        loadEvent.asDriver(onErrorJustReturn: false)
            .drive (onNext: { [weak self] isLoading in
                if(isLoading){
                    self?.showLoading()
                    return ;
                }
                self?.hideLoading()
            }, onDisposed: { [weak self] in
                self?.hideLoading()
            }).disposed(by: rx.disposeBag)
        
    }
    
    func login(_ phone : String ,_ code : String){
        print(">>>> login " )
        loadEvent.onNext(true)
    }
}
